package com.whfc.ms.api.fim.controller;

import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.fim.dto.FimResultAnalysisDTO;
import com.whfc.fim.dto.FimResultDTO;
import com.whfc.fim.dto.FimResultRateContDTO;
import com.whfc.fim.dto.FimWarnAnalysisDTO;
import com.whfc.fim.param.FimAlgResultDelParam;
import com.whfc.fim.param.FimResultBatchHandleParam;
import com.whfc.fim.param.FimResultEditParam;
import com.whfc.fuum.entity.SysUser;
import com.whfc.ms.api.fim.service.MsFimResultService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


/**
 * @Description: 智能监控 报警记录
 * @author: qzexing
 * @version: 1.0
 * @date: 2020/3/17 21:23
 */
@RequiresPermissions("fvs/fim")
@RestController
@RequestMapping("/ms/api/fim/result")
public class AppFimResultController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MsFimResultService msFimResultService;

    @GetMapping("/list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("pageNum") Integer pageNum,
                       @RequestParam("pageSize") Integer pageSize,
                       @RequestParam("deptId") Integer deptId,
                       @RequestParam("handleStatus") Integer handleStatus,
                       @RequestParam(value = "algCheckType", required = false) Integer algCheckType,
                       @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam(value = "startTime", required = false) Date startTime,
                       @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam(value = "endTime", required = false) Date endTime) {
        logger.info("报警记录-获取记录列表，loginer:{},pageNum:{},pageSize:{},deptId:{},handleStatus:{},algCheckType:{},startTime:{},endTime:{}",
                user.getUsername(), pageNum, pageSize, deptId, handleStatus, algCheckType, startTime, endTime);
        PageData<FimResultDTO> pageData = msFimResultService.list(pageNum, pageSize, deptId, handleStatus, algCheckType, startTime, endTime);
        return ResultUtil.success(pageData);
    }

    @RequiresPermissions("fvs/warnRecord/delete")
    @PostMapping("/del")
    public Result del(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @Validated @RequestBody FimAlgResultDelParam param) {
        logger.info("批量删除智能监控报警记录，loginer:{},param:{}", user.getUsername(), param.toString());
        msFimResultService.del(param);
        return ResultUtil.success();
    }

    @GetMapping("/detail")
    public Result detail(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                         @RequestParam("resultId") Integer resultId) {
        logger.info("报警记录-获取记录详情，loginer:{}resultId:{}", user.getUsername(), resultId);
        FimResultDTO resultDTO = msFimResultService.detail(resultId);
        return ResultUtil.success(resultDTO);
    }


    @PostMapping("/handle")
    public Result handle(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                         @RequestBody @Validated FimResultEditParam editParam) {
        logger.info("报警记录-处理报警记录，loginer:{}editParam:{}", user.getUsername(), editParam);
        editParam.setHandleUserId(user.getId());
        editParam.setHandleUserName(user.getNickname());
        msFimResultService.handle(editParam);
        return ResultUtil.success();
    }

    @RequiresPermissions("fvs/warnRecord/deal")
    @PostMapping("/batchHandle")
    public Result batchHandle(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                              @RequestBody @Validated FimResultBatchHandleParam param) {
        logger.info("报警记录-批量处理报警记录，loginer:{},param:{}", user.getUsername(), param.toString());
        param.setUserId(user.getId());
        param.setUserName(user.getNickname());
        msFimResultService.batchHandle(param);
        return ResultUtil.success();
    }

    @Deprecated
    @GetMapping(value = "/analysis", params = "date")
    public Result analysis(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                           @RequestParam("deptId") Integer deptId,
                           @RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {
        logger.info("报警记录-报警记录分析，loginer:{}deptId:{},date:{}",
                user.getUsername(), deptId, date);
        FimResultAnalysisDTO analysisDTO = msFimResultService.analysis(deptId, date);
        return ResultUtil.success(analysisDTO);
    }

    @Deprecated
    @GetMapping(value = "/analysis", params = {"startTime", "endTime"})
    public Result analysis(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                           @RequestParam("deptId") Integer deptId,
                           @RequestParam("startTime") @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
                           @RequestParam("endTime") @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime) {
        logger.info("报警记录-报警记录分析，loginer:{}deptId:{},startTime:{},endTime:{}",
                user.getUsername(), deptId, startTime, endTime);
        FimResultAnalysisDTO analysisDTO = msFimResultService.analysis(deptId, startTime, endTime);
        return ResultUtil.success(analysisDTO);
    }

    @GetMapping("/static")
    public Result warnNumstatic(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                @RequestParam("deptId") Integer deptId,
                                @RequestParam(value = "handleStatus", required = false) Integer handleStatus,
                                @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam(value = "startTime", required = false) Date startTime,
                                @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam(value = "endTime", required = false) Date endTime) {
        logger.info("报警次数统计,loginer:{},deptId:{},handleStatus:{},startTime:{},endTime:{}",
                user.getUsername(), deptId, handleStatus, startTime, endTime);
        ListData<FimResultRateContDTO> listData = msFimResultService.warnNumstatic(deptId, handleStatus, startTime, endTime);
        return ResultUtil.success(listData);
    }


    @GetMapping("/analysisByHour")
    public Result analysisByHour(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                 @RequestParam("deptId") Integer deptId,
                                 @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("date") Date date) {
        logger.info("一天的报警次数分析,loginer:{},deptId:{},date:{}", user.getUsername(), deptId, date);
        FimWarnAnalysisDTO data = msFimResultService.analysisByHour(deptId, date);
        return ResultUtil.success(data);
    }

    @GetMapping("/analysisByDate")
    public Result analysisByDate(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                 @RequestParam("deptId") Integer deptId,
                                 @DateTimeFormat(pattern = "yyyy-MM") @RequestParam("month") Date month) {
        logger.info("一个月的报警次数分析,loginer:{},deptId:{},month:{}", user.getUsername(), deptId, month);
        FimWarnAnalysisDTO data = msFimResultService.analysisByDate(deptId, month);
        return ResultUtil.success(data);
    }
}
